<template>
    <el-container class="ware_house_type_container">
        <el-main>
            <el-card class="box-card">
                <headerTemplate slot="header" ref="header"
                                :table-ref="tableOptions.ref"
                                :toolbar_buttons="headerOption.toolbar_buttons"
                                :toolbar_filter="headerOption.toolbar_filter"
                                :searchForm="searchForm"
                                @event="handleHeaderEvent">
                    <template slot="right_search">
                        <el-form-item label="编号" style="margin-bottom: 0;">
                            <el-input v-model="searchForm.value" style="width: 138px" @change="getList" clearable/>
                        </el-form-item>
                        <el-form-item label="名称" style="margin-bottom: 0;">
                            <el-input v-model="searchForm.displayText" style="width: 138px" @change="getList"
                                      clearable/>
                        </el-form-item>
                    </template>
                </headerTemplate>
                <vxeTableTemplate ref="xTable" :data="dataList" :options.sync="tableOptions"
                                  @event="handleTableEvent"/>
            </el-card>
        </el-main>
    </el-container>
</template>

<script>
import WareHouseTypeForm from "../dialog/WareHouseTypeForm";
import provide from "@common/mixins/provide";

export default {
    extends: provide,
    name: "JxcWarehouseType",
    data () {
        let _this = this;
        return {
            headerOption: {
                toolbar_buttons: [
                    {
                        id: 'refresh',
                        icon: 'el-icon-refresh-right',
                        show: true,
                        method: () => {
                            _this.getList(true);
                        },
                    },
                    {
                        id: 'add',
                        icon: 'fa fa-plus',
                        type: 'primary',
                        show: true,
                        permission: 'jxc:wareHouseType:save',
                        method: () => {
                            _this.openDialog(null, 'add');
                        },
                    },
                    {
                        id: 'edit',
                        icon: 'fa fa-pencil',
                        type: 'success',
                        show: true,
                        permission: 'jxc:wareHouseType:save',
                        method: () => {
                            if (!_this.checkSelection(_this.selectionList)) {
                                return;
                            }
                            _this.openDialog(_this.selectionList[0], 'edit');
                        },
                    },
                    {
                        id: "del",
                        icon: "fa fa-trash-o",
                        type: "danger",
                        show: true,
                        permission: 'jxc:wareHouseType:delete',
                        method: () => {
                            _this.deleteJxcWareHouse(null, 'batch');
                        },
                    },
                ],
                toolbar_filter: [],
            },
            searchForm: {},
            dataList: [],
            tableOptions: {
                ref: "wareHouseTypeTable",
                container: ".ware_house_type_container",
                multiSelect: true,
                columns: [
                    {
                        label: '编号',
                        prop: 'value',
                        align: 'center',
                        minWidth: 100,
                    },
                    {
                        label: '名称',
                        prop: 'display_text',
                        align: 'center',
                        minWidth: 100,
                    },
                    {
                        label: '备注',
                        prop: 'note_info',
                        align: 'center',
                    },
                ],
                operates: {
                    fixed: 'right',
                    width: 100,
                    list: [
                        {
                            id: "edit",
                            icon: "fa fa-pencil",
                            show: true,
                            permission: 'jxc:wareHouseType:save',
                            type: "success",
                            tooltip: {
                                content: "编辑",
                            },
                            method: (key, row, index) => {
                                _this.openDialog(row, "edit");
                            },
                        },
                        {
                            id: "del",
                            icon: "fa fa-trash-o",
                            type: "danger",
                            tooltip: {
                                content: "删除",
                            },
                            show: true,
                            permission: 'jxc:wareHouseType:delete',
                            method: (key, row, index) => {
                                _this.deleteJxcWareHouse(row, '');
                            },
                        },
                    ],
                },
                pagination: {
                    pageIndex: 1,
                },
            },
            selectionList: [],
        };
    },
    mounted () {
        this.initPage();
    },
    methods: {
        // 初始化
        initPage () {
            this.getList(true);
        },
        // 刷新、查询
        handleHeaderEvent (event) {
            let _this = this;
            _this.searchForm = $.extend({}, _this.searchForm, event.searchForm);
            if (event.type === "search") {
                _this.getList(true);
            } else if (event.type === "refresh") {
                _this.getList(true);
            }
        },
        // 获取列表信息
        getList (query) {
            let _this = this;
            if (query) {
                _this.tableOptions = _this.resetTablePagination(_this.tableOptions);
            }
            let queryData = _this.getQueryParams(_this.tableOptions, _this.searchForm);
            queryData.key1 = _this.$api.STOCK_WAREHOUSE_TYPE_KEY;
            _this.fetchData(_this.$api.API_FindCommonParameters, queryData).then(result => {
                _this.dataList = result.data || [];
                _this.tableOptions = _this.setTableTotal(_this.tableOptions, result);
            }).catch(() => {

            });
        },
        handleTableEvent (event) {
            if (event.type === 'page') {
                this.getList();
                return;
            }
            if (event.type === 'selectionChange') {
                this.selectionList = event.value;
            }
        },
        // 新增/编辑 Dialog
        openDialog (row, type) {
            let _this = this;
            let title = type === 'add' ? '新增仓库类型' : '编辑仓库类型';
            if (type === 'add') {
                row = {};
            }
            _this.$dialog({
                title,
                width: '620px',
                component: {
                    value: WareHouseTypeForm,
                    props: {
                        row,
                    },
                },
                confirmed () {
                    _this.getList();
                },
            });
        },
        // 删除
        deleteJxcWareHouse (row, type) {
            let _this = this;
            let sids = [];
            if (type === 'batch') {
                if (!_this.checkSelection(_this.selectionList, true)) {
                    return;
                }
                sids = _this.selectionList.map(item => item.sid);
            } else {
                sids.push(row.sid);
            }
            _this.confirmDel(sids);
        },
        confirmDel (sids) {
            let _this = this;
            _this.$confirm("是否继续删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                _this.fetchData(_this.$api.API_DeleteCommonParameterValues, {sids}).then(result => {
                    _this.getList(true);
                    _this.$message.success("删除成功！");
                }).catch((error) => {
                    _this.$message.error(error.message);
                });
            }).catch(() => {

            });
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.ware_house_type_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
